﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;

using Microsoft.Win32;

namespace LogoScriptIDE
{
    /// <summary>
    /// Interaction logic for ConsoleWindow.xaml
    /// 控制台窗口
    /// </summary>
    public partial class ConsoleWindow : UserControl
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public ConsoleWindow()
        {
            InitializeComponent();
            Write("LogoScript Demo\n");
            Write("BY: ak48disk, biubiubiu, 姜帆爱杨幂, 妹纸诚觅交大男 & bra_na_na\n");
        }

        /// <summary>
        /// 字符串构造器，用于缓冲输入
        /// </summary>
        private StringBuilder m_sb = new StringBuilder();
        
        /// <summary>
        /// 向控制台写字符串
        /// </summary>
        /// <param name="str">待写入的字符串</param>
        public void Write(string str)
        {
            //将字符串写入到缓冲区中
            m_sb.Append(str);
        }

        /// <summary>
        /// 计时器，用于刷新屏幕
        /// </summary>
        private System.Windows.Threading.DispatcherTimer m_timer;
        
        /// <summary>
        /// 载入完成回调函数
        /// </summary>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            // 新建计时器，每个一段时间将缓冲区字符串刷新到界面中
            m_timer = new System.Windows.Threading.DispatcherTimer(
                System.Windows.Threading.DispatcherPriority.Send);
            m_timer.Tick += new EventHandler(TimeTick);
            m_timer.Interval = new TimeSpan(0, 0, 0, 0, 50);
            m_timer.Start();
        }

        /// <summary>
        /// 计时器时间到回调函数
        /// </summary>
        private void TimeTick(object sender, EventArgs e)
        {
            // 只有当缓冲区中存在字符时才刷新
            if (m_sb.Length != 0)
            {
                // 将缓冲区内容添加到文本框中
                ui_textBox.AppendText(m_sb.ToString());
                // 清空缓冲区
                m_sb = new StringBuilder();
                // 滚动到最后一行
                ui_textBox.ScrollToEnd();
            }
        }

        /// <summary>
        /// 右键菜单清空回调函数
        /// </summary>
        private void ui_mnuClear_Click(object sender, RoutedEventArgs e)
        {
            // 同时清空缓冲区和文本框
            m_sb.Clear();
            ui_textBox.Clear();
        }

        /// <summary>
        /// 右键菜单保存回调函数
        /// </summary>
        private void ui_mnuSave_Click(object sender, RoutedEventArgs e)
        {
            Save();
        }

        /// <summary>
        /// 保存控制台信息到文件
        /// </summary>
        public void Save()
        {
            try
            {
                // 显示保存文件窗口
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Filter = "Text documents (.txt)|*.txt";
                saveFileDialog.FileName = "console.txt";
                saveFileDialog.DefaultExt = ".txt";

                // 若用户选择保存，则将文本框信息写入到对应文件中
                if (saveFileDialog.ShowDialog() == true)
                {
                    TextWriter writer = new StreamWriter(saveFileDialog.FileName);
                    writer.Write(ui_textBox.Text);
                    writer.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("FAILED TO SAVE FILE :\n" + ex.Message);
            }
        }
    }
}
